<?php
/**
 * Created by PhpStorm.
 * User: yangyulu
 * Date: 2020/3/3
 * Time: 下午4:03
 */

class Admin_voteController extends Admin_baseController{
    public function listAction(){
        $page = Request::getParams('page', 1);
        $pageSize = Request::getParams('pageSize', 20);
        $title=Request::getParams("title");
        $where = $value = $url = array();
        if ($title) {
            $where[] = 'problem like ?';
            $value[] = '%' . $title . '%';
            $url['problem'] = $title;
        }
        $total = self::model('vote')->count('id', $where, $value);
        $list = self::model('vote')->limit(($page - 1) * $pageSize, $pageSize)->order('id desc')->getAll($where, $value);
        $this->assign(array(
            'page' => $page,
            'pageSize' => $pageSize,
            'title' => $title,
            //'status' => $status,
            'statusList' => self::model('user')->getAllStatus(),
            'total' => $total,
            'list' => $list,
            'pageHtml' => self::instance('Pagination')->page($page)->num($pageSize)->total($total)->url(Router::getBaseUrl() . '/admin_vote/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml(),
        ));
        $this->display();
    }

    public function voteOptionAction(){
        $vote_id=Request::getParams("ids");
        $list=self::model("vote_option")->getAll(["vote_id=?","is_delete=?"],[$vote_id,0]);
        $this->assign("list",$list);
        $this->display();
    }

    public function applyAction(){
        $ids=Request::getParams("voteId");
        $res=self::model("vote")->Exhibition($ids);
        if($res){
            Response::ajax(200, "展示成功");
        }else{
            Response::showMsg('展示失败');
        }
    }

    public function editAction(){
        $option_id = Request::getParams('option_id');
        $option=Request::getParams("option");
        $problem=Request::getParams("problem");
        $voteId = Request::getParams('voteId');
        if ($voteId && !$info = self::model('vote')->getOne('id=?', $voteId)) {
            Response::showMsg('信息查询失败，请稍后重试');
        }
        if (Request::isGet()) {
            if(!empty($info)){
                $voteOption=self::model("vote_option")->getAll(["vote_id=?"],[$voteId]);
            }
            $this->assign([
                'info' => $info,
                'voteOption' => $voteOption,
            ]);

            $this->display();
            exit;
        }

        if(empty($problem)){
            Response::ajax(NORMAL_ERROR,'请填写投票问题');
        }
        if(empty($voteId)){
            $vote_data["problem"]=$problem;
            $vote_data["add_time"]=time();
            $vote_data["update_time"]=time();
            $ids = self::model('vote')->insert($vote_data, true);
            foreach ($option_id as $k=>$v){
                if(empty($option[$k])&&$k==0){
                    Response::showMsg('请填写完整选项');
                }
                $add_data["vote_option"]=$option[$k];
                $add_data["vote_id"]=$ids;
                $add_data["add_time"]=time();
                $add_data["update_time"]=time();
                self::model("vote_option")->insert($add_data,true);
            }
            Response::ajax(200, '添加成功');
        }else{
            $vote_data["problem"]=$problem;
            $vote_data["update_time"]=time();
            self::model('vote')->update($vote_data, ["id=?"],[$voteId]);
            foreach ($option_id as $k=>$v){
                if(empty($option[$k])&&$k==0){
                    Response::showMsg('请填写完整选项');
                }
                $info=self::model("vote_option")->getOne(["id=?"],[$v]);
                if(empty($info)){
                    $add_data["vote_option"]=$option[$k];
                    $add_data["vote_id"]=$voteId;
                    $add_data["add_time"]=time();
                    $add_data["update_time"]=time();
                    self::model("vote_option")->insert($add_data,true);
                }else{
                    $add_data["vote_option"]=$option[$k];
                    $add_data["update_time"]=time();
                    self::model("vote_option")->update($add_data,["id=?"],$v);
                }

            }
            Response::ajax(200, '修改成功');
        }



    }
}